package com.linguochao.rabbitmq.comsumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * 批量单据消费者
 *
 * @author linguochao
 * @date 2022/5/19
 */
@Component
@Slf4j
public class BatchQueueListener {



    //批量接收处理
    @RabbitListener(queues = "#{'${spring.rabbitmq.batchQueue}'.split(',')[0]}", containerFactory = "batchQueueRabbitListenerContainerFactory")
    public void onMessageBatch1(List<Message> list, Channel channel) {

        System.out.println("hash1");
        System.out.println("size:" + list.size());
        for (Message message : list) {
            Long retryCount = getRetryCount(message.getMessageProperties());
            log.info("retryCount:{}", retryCount);
            String s = new String(message.getBody());
            System.out.println(s);

            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    //批量接收处理
    @RabbitListener(queues = "#{'${spring.rabbitmq.batchQueue}'.split(',')[1]}", containerFactory = "batchQueueRabbitListenerContainerFactory")
    public void onMessageBatch2(List<Message> list, Channel channel) {

        System.out.println("hash2");
        System.out.println("size:" + list.size());
        for (Message message : list) {
            Long retryCount = getRetryCount(message.getMessageProperties());
            log.info("retryCount:{}", retryCount);
            String s = new String(message.getBody());
            System.out.println(s);

            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    //批量接收处理
    @RabbitListener(queues = "#{'${spring.rabbitmq.batchQueue}'.split(',')[2]}", containerFactory = "batchQueueRabbitListenerContainerFactory")
    public void onMessageBatch3(List<Message> list, Channel channel) {

        System.out.println("hash3");
        System.out.println("size:" + list.size());
        for (Message message : list) {
            Long retryCount = getRetryCount(message.getMessageProperties());
            log.info("retryCount:{}", retryCount);
            String s = new String(message.getBody());
            System.out.println(s);

            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    /**
     * 获取消息被重试的次数
     *
     * @param messageProperties Message
     * @return 重试次数
     */
    private Long getRetryCount(MessageProperties messageProperties) {
        Long retryCount = 0L;
        if (null != messageProperties) {
            List<Map<String, ?>> deaths = messageProperties.getXDeathHeader();
            if (deaths != null && deaths.size() > 0) {
                Map death = deaths.get(0);
                retryCount = (Long) death.get("count");
            }
        }
        return retryCount;
    }


}
